let http = require('http');
let fs = require('fs');
let path = require('path');

let server = http.createServer(function(request,response){
    // response.write("<html>");
    // response.write("<head>");
    // response.write("<meta charset=utf-8>");
    // response.write("</head>");
    // response.write("<body>");
    // response.write("<h1>你好 NodeJS!<h1>");
    // response.write("</body>");
    // response.write("</html>");
    console.log("request.url:",request.url);
    let url = new URL("http://localhost:3000" + request.url);
    // console.log("url",url)
    let searchParams = url.searchParams;
    if(url.pathname == "/login"){
        
        if(searchParams.get("username") == "aaaaaa"
            && searchParams.get("pwd") == "123456"
        ){
            response.write("suc");
        }else{
            response.write("fail");
        }
    }else if(url.pathname == "/reg"){
        console.log("注册用户名：",searchParams.get("username"));
        console.log("注册密码：",searchParams.get("pwd"));
        response.write("reg suc");
    }else{
        try{
            let data = fs.readFileSync(path.join('public',request.url));
            response.write(data);
        }catch(err){}
    }
   
    
    response.end();
});

// 启动服务器，并指定监听端口
server.listen(3000);
console.log("服务器启动成功");
